<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 pane:面-->
          <!-- label是每个标签页的名字 -->
          <!-- 左侧内容 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog = true"
                >新增角色
              </el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :border="true" :data="list">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template v-slot="{ row }">
                  <el-button
                    size="small"
                    type="success"
                    @click="assignPerm(row.id)"
                    >分配权限</el-button
                  >
                  <el-button
                    size="small"
                    type="primary"
                    @click="editRole(row.id)"
                    >编辑</el-button
                  >
                  <el-button
                    size="small"
                    type="danger"
                    @click.native="deleteRole(row.id)"
                    >删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 prev:左箭头上一页，next：右箭头下一页，pager：页码列表-->
              <el-pagination
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                @current-change="changePage"
                layout="prev,pager,next"
              />
            </el-row>
          </el-tab-pane>
          <!-- 右侧内容 -->
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单区域 -->
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="formData.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="formData.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="formData.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="formData.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹层 -->
      <el-dialog title="编辑弹层" :visible="showDialog" @close="btnCancel">
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK"
              >确定</el-button
            >
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限弹出层 -->
      <el-dialog
        title="分配权限"
        :visible="showPermDialog"
        @close="btnPermCancel"
      >
        <!-- 权限是一颗树 -->
        <!-- 将数据绑定到组件上 -->
        <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
        <!-- id作为唯一标识 -->
        <el-tree
          :data="permData"
          :props="defaultProps"
          :default-expand-all="true"
          :show-checkbox="true"
          :check-strictly="true"
          :default-checked-keys="selectCheck"
          node-key="id"
          ref="permTree"
        />
        <!-- 确定 取消 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK"
              >确定</el-button
            >
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { tranListToTreeData } from "@/utils";
import { getPermissionList } from "@/api/permission";
import {
  getRoleListAPI,
  getCompanyInfoAPI,
  deleteRoleAPI,
  editRoleAPI,
  getRoleDetailAPI,
  addRoleAPI,
  assignPerm,
} from "@/api/setting";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      list: [], // 承接数组
      page: {
        // 放置页码及相关数据
        page: 1, //当前页
        pagesize: 5, //每页条目数
        total: 0, // 记录总数
      },
      // 定义表单数据
      formData: {},
      // 控制弹层
      showDialog: false,
      // 专门接收新增或者编辑的编辑的表单数据
      roleForm: {
        name: "",
        description: "",
      },
      rules: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
      },
      showPermDialog: false, // 控制分配权限弹层的显示后者隐藏
      defaultProps: {
        label: "name",
      },
      permData: [], // 专门用来接收权限数据 树形数据
      selectCheck: [], // 定义一个数组来接收 已经选中的节点
      roleId: null, // 用来记录分配角色的id
    };
  },
  created() {
    // 组件创建后请求角色信息
    this.getRoleList();
    // 组件创建后获取公司信息
    this.getCompanyInfo();
  },
  methods: {
    async getRoleList() {
      const { rows, total } = await getRoleListAPI(this.page);
      this.list = rows;
      this.page.total = total;
    },
    changePage(newPage) {
      this.page.page = newPage;
      this.getRoleList();
    },
    // 获取公司信息
    async getCompanyInfo() {
      this.formData = await getCompanyInfoAPI(this.companyId);
    },
    // 删除角色回调
    async deleteRole(id) {
      try {
        await this.$confirm("确定删除该角色!", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        });
        await deleteRoleAPI(id);
        this.$message.success("删除角色成功!");
        // 重新请求一次角色信息
        this.getRoleList();
      } catch (error) {
        if (error === "cancel") {
          this.$message.info("取消");
        } else {
          this.$message.error(error || "出错了请稍后重试");
        }
      }
    },
    // 编辑角色回调
    async editRole(id) {
      // 点击后 显示弹出层
      this.showDialog = true;
      // 读取角色信息
      this.roleForm = await getRoleDetailAPI(id);
    },
    // 点击确定后的回调
    async btnOK() {
      const { roleForm, $message } = this;
      try {
        await this.$refs.roleForm.validate();
        if (roleForm.id) {
          // 编辑业务
          await editRoleAPI(roleForm);
          $message.success("更新成功");
        } else {
          // 新增业务
          await addRoleAPI(roleForm);
          $message.success("添加成功");
        }

        // 重新拉取数据
        this.getRoleList();
        this.showDialog = false;
      } catch (error) {
        if (error === "cancel") {
          $message.info("取消");
        } else {
          $message.error(error || "出错了请稍后重试");
        }
      }
    },
    // 取消后关闭弹出层
    btnCancel() {
      this.roleForm = {
        name: "",
        description: "",
      };
      // 移除校验
      this.$refs.roleForm.resetFields();
      this.showDialog = false;
    },
    // 分配权限按钮回调
    async assignPerm(id) {
      //获取所有权限数据，并且 转化list到树形数据
      this.permData = tranListToTreeData(await getPermissionList(), "0");
      this.roleId = id; //保存当前角色id
      // 应该去获取 这个id的 权限点
      const { permIds } = await getRoleDetailAPI(id); // permIds是当前角色所拥有的权限点数据
      this.selectCheck = permIds; // 将当前角色所拥有的权限id赋值
      this.showPermDialog = true;
    },
    // 分配权限 弹层 确定回调
    async btnPermOK() {
      await assignPerm({
        permIds: this.$refs.permTree.getCheckedKeys(),
        id: this.roleId,
      });
      this.$message.success("分配权限成功");
      this.showPermDialog = false;
    },
    // 分配权限 弹层 取消回调
    btnPermCancel() {
      this.selectCheck = []; // 重置数据
      this.showPermDialog = false;
    },
  },
  computed: {
    ...mapGetters(["companyId"]),
  },
};
</script>

<style>
</style>